package com.yitong.xjd.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.yitong.xjd.domain.Test;

@Repository
public class TestDaoImpl implements TestDao{
	@Autowired
	@Qualifier("firstJdbcTemplate")
    private JdbcTemplate jdbcTemplate;
	
	@Override
	public Test findTestByName(String name) {
		return jdbcTemplate.queryForObject("select c.* from Test c where c.name=?", new Object[]{name}, new RowMapper<Test>(){
			@Override
			public Test mapRow(ResultSet rs, int rowNum) throws SQLException {
				Test t = new Test();
				t.setId(rs.getString("id"));
				t.setName(rs.getString("name"));
				t.setValue(rs.getString("value"));
				return t;
			}
		});
	}
	
	@Override
    public List<Test> getPageList(int start,int end) {
        return jdbcTemplate.query("select id, name, value from ( select row_.*, rownum rownum_ from test row_ where rownum <= ? ) where rownum_ > ?",new Object[]{end,start},new RowMapper<Test>(){
			@Override
			public Test mapRow(ResultSet rs, int rowNum) throws SQLException {
				Test t = new Test();
				t.setId(rs.getString("id"));
				t.setName(rs.getString("name"));
				t.setValue(rs.getString("value"));
				return t;
			}
		});
    }

}